package com.oracle.application.controller;

import com.oracle.application.config.PayConfig;
import com.oracle.application.entity.vo.PayAsyncVo;
import com.oracle.application.entity.vo.PayVo;
import com.oracle.application.entity.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 莉丝妲黛
 */
@RestController
public class PayController {

    @Autowired
    private PayConfig payConfig;

    /**
     * 发起支付请求
     *
     * @param orderSn    订单编号
     * @param totalPrice 订单总价
     * @param orderId    订单id
     * @return 返回请求页面
     */
    @GetMapping("/pay")
    public String pay(String orderSn, String totalPrice, Integer orderId) {
        PayVo payVo = new PayVo();
        payVo.setOut_trade_no(orderSn);
        payVo.setTotal_amount(totalPrice);
        payVo.setSubject("创世结晶6480");
        //payVo.setPassback_params("");
        String page = this.payConfig.doPay(payVo, orderId);
        System.out.println("----------------------------------------------------------------");
        return page;
    }


    /**
     * 支付宝支付后异步的请求回调地址 用户 验签 和业务处理
     * 并返回给支付宝业务的状态当核心业务有问题时 必须返回给支付宝失败
     * 非核心问题不必返回失败
     *
     * @param payAsyncVo
     * @return
     */
    @PostMapping("/pay/notify_url")
    public String notify_url(PayAsyncVo payAsyncVo) {
        if (this.payConfig.verifyPaySignTrue(payAsyncVo)) {
            return "success";
        }
        return "failuer";
    }

    @GetMapping("/pay/return_url")
    public R return_url(PayAsyncVo payAsyncVo) {
        if (this.payConfig.returnVerifySignTrue(payAsyncVo)) {
            return new R(200, "success", "支付完成等待发货");
        }
        return new R(201, "fail", "支付异常,请核实后再次支付");
    }

}
